package com.cgsd.heyu.ui.info;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.library.adapter.ClickItemListener;
import com.android.library.utils.SharedPreferencesHelper;
import com.android.library.utils.StringUtils;
import com.android.library.utils.SystemUtil;
import com.android.library.utils.ToastHelper;
import com.cgsd.heyu.App;
import com.cgsd.heyu.R;
import com.cgsd.heyu.adapter.CenterContentAdapter;
import com.cgsd.heyu.api.AppConfig;
import com.cgsd.heyu.common.AppMobclickAgent;
import com.cgsd.heyu.common.CommonConfig;
import com.cgsd.heyu.common.Constants;
import com.cgsd.heyu.entity.TabEntity;
import com.cgsd.heyu.presenter.user.UserContract;
import com.cgsd.heyu.presenter.user.UserPresenter;
import com.cgsd.heyu.ui.BaseActivity;
import com.cgsd.heyu.ui.login.LoginActivity;
import com.cgsd.heyu.ui.webview.CommonH5Activity;
import com.cgsd.heyu.utils.CacheUtil;
import com.cgsd.heyu.widgets.dialog.ConfirmDialog;
import com.hztz.kankanzhuan.KanNewsSDK;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xiaoxing
 * Description 说明
 * CreateData: 2020/11/4
 */
public class SettingActivity extends BaseActivity<UserContract.Presenter> implements ClickItemListener, View.OnClickListener, UserContract.SettingView {
    /**
     * 意见反馈
     */
    public static final String FEED_BACK = "feedback";
    /**
     * 清除缓存
     */
    public static final String CLEAR_CACHE = "clear_cache";
    /**
     * 粉丝QQ群
     */
    public static final String QQ_GROUP = "fans_qun";
    /**
     * 客服微信号
     */
    public static final String WEIXIN_NO = "official_wechart";
    /**
     * 用户协议
     */
    public static final String USER_AGREEMENT = "user_agreement";
    /**
     * 隐私协议
     */
    public static final String PRIVACY_PROTOCOL = "privacy_protocol";
    /**
     * 版本更新
     */
    public static final String VERSION_UPDATE = "version_UPDATE";
    /**
     * 退出登录
     */
    public static final String SIGN_OUT = "1";
    /**
     * 注销账户
     */
    public static final String LOGOUT = "2";
    private CenterContentAdapter adapter;
    private List<TabEntity> data = new ArrayList<>();
    private ConfirmDialog sigOutDialog;
    private ConfirmDialog logoutDialog;
    private TextView tvLogout;
    private boolean isNewVersion;

    public static void actionStart(Activity activity){
        AppMobclickAgent.onEvent(AppMobclickAgent.SHEZHIYE);
        Intent intent = new Intent(activity,SettingActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_settting;
    }

    @Override
    protected void initView() {
        new UserPresenter(this);
        tvLogout = findViewById(R.id.tv_logout);
        RecyclerView mRecyclerView = findViewById(R.id.mRecyclerView);
        adapter = new CenterContentAdapter(this,data);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(adapter);

        adapter.setClickItemListener(this);
        findViewById(R.id.tv_sign_out).setOnClickListener(this);
        tvLogout.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        isNewVersion = SharedPreferencesHelper.getBoolean(Constants.ShareKeyValue.IS_NEW_VERSION);
        String cacheNumber = null;
        try {
            cacheNumber = CacheUtil.getTotalCacheSize(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        data.add(new TabEntity(getString(R.string.feedback),FEED_BACK,null));
        data.add(new TabEntity(getString(R.string.clear_cache),CLEAR_CACHE,cacheNumber));
//        data.add(new TabEntity(getString(R.string.qq_group),QQ_GROUP,CommonConfig.getInstance().qqGroup));
        data.add(new TabEntity(getString(R.string.weixin_no),WEIXIN_NO,CommonConfig.getInstance().weixinNo));
        data.add(new TabEntity(getString(R.string.user_agreement),USER_AGREEMENT,null));
        data.add(new TabEntity(getString(R.string.privacy_agreement),PRIVACY_PROTOCOL,null));
        if (isNewVersion) {
            data.add(new TabEntity(getString(R.string.version_update), VERSION_UPDATE, getString(R.string.current_version_newest)));
        }else {
            data.add(new TabEntity(getString(R.string.version_update), VERSION_UPDATE, getString(R.string.current_is_new_version)));
        }
        adapter.notifyDataSetChanged();
        if (StringUtils.isEmpty(CommonConfig.getInstance().downH5Url)){
            mPresenter.getKeyConfig(CommonConfig.SHARE_DOWNLOAD_H5_URL);
        }
        if (CommonConfig.getInstance().isShowLogout){
            tvLogout.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void clickItem(View view, int position) {
        TabEntity entity = data.get(position);
        switch (data.get(position).type){
            case FEED_BACK:
                AppMobclickAgent.onEvent(AppMobclickAgent.SHEZHI_FANKUI);
                FeedBackActivity.actionStart(this);
                break;
            case CLEAR_CACHE:
                AppMobclickAgent.onEvent(AppMobclickAgent.SHEZHI_HUANCUN);
                CacheUtil.clearAllCache(this);
                entity.other = "0M";
                adapter.notifyDataSetChanged();
                break;
            case QQ_GROUP:
                AppMobclickAgent.onEvent(AppMobclickAgent.SHEZHI_QQQUN);
                SystemUtil.copyStr(App.getInstance(),entity.other);
                ToastHelper.showDefaultToast("复制成功");
                showToast(getString(R.string.copy_success));
                break;
            case WEIXIN_NO:
                AppMobclickAgent.onEvent(AppMobclickAgent.SHEZHI_WEIXINHAO);
                SystemUtil.copyStr(App.getInstance(),entity.other);
                ToastHelper.showDefaultToast("复制成功");
                showToast(getString(R.string.copy_success));
                break;
            case VERSION_UPDATE:
                AppMobclickAgent.onEvent(AppMobclickAgent.SHEZHI_JIANCHA);
                if (isNewVersion){
                    if (StringUtils.isEmpty(CommonConfig.getInstance().downH5Url)){
                        showToast("数据刷新错误，请退出重试");
                    }else {
                        //从其他浏览器打开
                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_VIEW);
                        Uri contentUrl = Uri.parse(CommonConfig.getInstance().downH5Url);
                        intent.setData(contentUrl);
                        startActivity(Intent.createChooser(intent, "请选择浏览器"));
                    }
                }
                break;
            case USER_AGREEMENT:
                //用户协议
                AppMobclickAgent.onEvent(AppMobclickAgent.SHEZHI_YONGHU);
                CommonH5Activity.actionStart(this, AppConfig.USER_AGREEMENT,getString(R.string.user_agreement));
                break;
            case PRIVACY_PROTOCOL:
                //隐私协议
                AppMobclickAgent.onEvent(AppMobclickAgent.SHEZHI_YINSI);
                CommonH5Activity.actionStart(this,AppConfig.PRIVACY_PROTOCOL,getString(R.string.privacy_agreement));
                break;
            default:
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_sign_out:
                AppMobclickAgent.onEvent(AppMobclickAgent.SHEZHI_TUICHU);
                showSignOutDialog();
                break;
            case R.id.tv_logout:
                AppMobclickAgent.onEvent(AppMobclickAgent.SHEZHI_ZHUXIAO);
                showLogoutDialog();
                break;
            default:
        }
    }

    /**
     * 退出账号
     */
    private void showSignOutDialog(){
        if (sigOutDialog == null){
            sigOutDialog = new ConfirmDialog(this);
            sigOutDialog.setTitle(getString(R.string.confirm_sign_out));
            sigOutDialog.setDialogListener(new ConfirmDialog.DialogListener() {
                @Override
                public void confirm() {
                    sigOutDialog.dismiss();
                    mPresenter.signOut(SIGN_OUT);
                }

                @Override
                public void cancel() {
                    sigOutDialog.dismiss();
                }
            });
        }
        sigOutDialog.show();
    }

    /**
     * 注销
     */
    private void showLogoutDialog(){
        if (logoutDialog == null){
            logoutDialog = new ConfirmDialog(this);
            logoutDialog.setTitle(getString(R.string.confirm_logout));
            logoutDialog.setDialogListener(new ConfirmDialog.DialogListener() {
                @Override
                public void confirm() {
                    logoutDialog.dismiss();
                    mPresenter.signOut(LOGOUT);
                }

                @Override
                public void cancel() {
                    logoutDialog.dismiss();
                }
            });
        }
        logoutDialog.show();
    }


    @Override
    public void signOutSuccess(String type) {
        if (LOGOUT.equals(type)) {
            showToast(getString(R.string.logout_success));
        }else {
            showToast(getString(R.string.sig_out_success));
        }
        KanNewsSDK.getInstance().logout();
        LoginActivity.actionStart(this);
    }
}
